const http = require('../utils/http.js');
const getYGDomain = function () {
    let app = getApp() || this;
    // return `https://ygapi.meb.${app.globalData.isDev ? 'im' : 'com'}/api`;
    // return `${app.globalData.isDev?'http://47.114.5.153:8082':'htps://tntbeauty.cn'}`
    return `${app.globalData.isDev?'http://47.114.5.153:8082':'https://tntbeauty.cn/'}`
};
const qrcode = '';

/**
 * 获取验证码
 */
const getCode = (mobile) => {
    return http.request('GET', '/v1/User/GetCode', mobile);
}

/**
 * 登录
 */
const login = (model) => {
    return http.request('POST', '/v1/User/Login', model)
}



/**
 * 微信一键登录
 */
const wxLogin = (model) => {
    return http.request('POST', '/v1/User/WeixinLogin', model)
}

/**
 * 获取七牛token
 */
const getQiNiuToken = () => {
    return http.request('GET', '/v1/Tool/GetQiniuToken');
};

/**
 * 获取保存的公用设置数据
 */
const getCommonSetting = (id) => {
    return http.request('GET', '/v1/Tool/GetSettingsByKey?key=' + id);
}

/**
 * 获取用户是否收藏小程序弹窗状态
 */
const getCollectionTips = () => {
    return http.request('GET', '/v1/User/GetCollectionTips');
}

/**
 * 隐藏收藏小程序弹窗
 */
const hideCollectionTips = () => {
    return http.request('POST', '/v1/User/HideCollectionTips');
}

/**
 * 根据userid获取用户信息（无需登录）
 */
const getUserInfo = (id) => {
    return http.request('GET', '/v1/User/GetUserBaseInfo?userId=' + id)
}

const getSrc = (data)=>{
    console.log(data);
    return http.request('POST', `/goods/goods_qrcode`, data);
}

/**
 * 获取小程序码
 */
async function getMiniCode(data)  {
    try {
        var res = await getSrc(data);
        var src = res.data.ResData;
        console.log(src);
    }catch(e){
        console.log(e,'获取src地址失败')
    }
    
    console.log(res,'res....')
    return new Promise((resolve, reject) => {
        console.log(src,'src')
        let obj = { src };
        console.log(obj, 'obj')
        wx.getImageInfo({
            src,
            success(res) {
                console.log(res, 'base res')
                obj.info = res;
                resolve(obj);
            },
            fail(err) {
                console.log(err, 'err base')
                reject(err);
            }
        })
    });
}

/**
 * 获取小程序码在redis中的数据
 */
const getMiniCodeData = (id) => {
    return http.request('GET', getYGDomain() + '/server/v2.0/home/GetWxCodeData?id=' + id)
}

/**
 * 获取用户openid
 */
const getUserOpenid = function (params) {
    return http.request.call(this, 'GET', '/WeiXin/WxOpen/LoginByOpenId', params)
}


/**
 * 获取服务器时间
 */
// const getServerTime = function () {
//     return http.request.call(this, 'GET', getYGDomain.call(this) + '/gf/v1.0/home/GetServerTime')
// }

/**
 * 错误日志上报
 */
// const errorLog = function (params) {
//     return http.request.call(this, 'POST', '/v1/Home/AddLog', params)
// }

/**
 * 获取用户状态
 */
const getUserStatus = function (id) {
    return http.request.call(this, 'GET', '/v1/User/GetUserStatus?userId=' + id)
}

/**
 * 微信wx.login成功之后调用
 */
const wxOnLogin = (data) => {
    // return http.request('POST', `/user/login`, data)
    return http.request('POST', `/WeiXin/WxOpen/OnLogin`, data)

}


// 收集formid订阅消息
const addSubscribe = (data) => {
    return http.request('POST',`/Common/whchatFormId`, data)
}


module.exports = {
    getCode,
    login,
    wxLogin,
    getMiniCode,
    getMiniCodeData,
    getQiNiuToken,
    getCollectionTips,
    hideCollectionTips,
    getCommonSetting,
    getUserInfo,
    getUserOpenid,
    // getServerTime,
    // errorLog,
    getUserStatus,
    wxOnLogin,
    addSubscribe
}
